from openai import OpenAI
import os

def get_response():
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"),  # 从环境变量读取
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    )
    completion = client.chat.completions.create(
        model="qvq-max",  # 使用通义千问-QVQ-Max 的 model code
        messages=[
            {
                "role": "system",
                "content": [{"type": "text", "text": "You are a helpful assistant."}]
            },
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": "https://img1.baidu.com/it/u=4083093091,1072394061&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=753"
                        }
                    },
                    {
                        "type": "text",
                        "text": "分析老人的情绪"
                    }
                ]
            }
        ],
        stream=True
    )
    full_response = ""
    try:
        for chunk in completion:
            if chunk.choices[0].delta.content:
                full_response += chunk.choices[0].delta.content
                print(chunk.choices[0].delta.content, end='', flush=True)
    except Exception as e:
        print(f"\n请求过程中发生错误：{e}")
    finally:
        print(full_response)

if __name__ == '__main__':
    get_response()